package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import com.itheima.utils.StringNameUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：why
 * @description：
 * @version:
 */
@RequestMapping("/menu")
@RestController
public class MenuController {

    @Reference
    MenuService menuService;

    // 分页和条件查询
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return menuService.pageQuery(queryPageBean);
    }


    // 获取所有的父级menu
    @GetMapping("/findAllParentMenu")
    public Result findAllParentMenu() {
        try {
            List<Menu> allParentMenu = menuService.findAllParentMenu();
            return new Result(true, MessageConstant.GET_MENU_PARENT_SUCCESS, allParentMenu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_PARENT_FAIL);
        }
    }

    @GetMapping("/findAllIcons")
    public Result findAllIcons() {
        try {
            List<Menu> allIconsMenu = menuService.findAllIcons();
            return new Result(true, MessageConstant.GET_MENU_ICONS_PARENT_SUCCESS, allIconsMenu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_ICONS_PARENT_FAIL);
        }
    }


    @PostMapping("/edit")
    public Result edit(@RequestBody Menu menu) {
        boolean result = false;
        String regx = "[`~!@#$^&*()=|{}':;,\\[\\].<>《》/?！￥…（）—【】‘；：”“。，、？]";
        try {
            if (menu.getName() != null && !"".equals(menu.getName()) && !menu.getName().contains(" ") && !"".matches(regx))
                result = menuService.edit(menu);
            if (result) {
                return new Result(result, MessageConstant.EDIT_MENU_SUCCESS);
            } else {
                return new Result(result, MessageConstant.EDIT_MENU_FAIL);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(result, MessageConstant.EDIT_MENU_FAIL);
        }
    }


    @PostMapping("/add")
    public Result add(@RequestBody Menu menu) {
        boolean result = false;
        String regx = "[`~!@#$^&*()=|{}':;,\\[\\].<>《》/?！￥…（）—【】‘；：”“。，、？]";
        try {
            if (StringNameUtils.checkName(menu.getName()))
                result = menuService.add(menu);
            if (result) {
                return new Result(result, MessageConstant.ADD_MENU_SUCCESS);
            } else {
                return new Result(result, MessageConstant.ADD_MENU_FAIL);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(result, MessageConstant.ADD_MENU_FAIL);
        }
    }

    @GetMapping("/delete")
    public Result delete(Integer id) {
        return menuService.delete(id);
    }


    @GetMapping("/findUserMenuByUerName")
    public Result findUserMenuByUerName() {
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = user.getUsername();
            List<Menu> userMenuByUerName = menuService.findUserMenuByUerName(username);
            return new Result(true, MessageConstant.GET_USER_MENU_SUCCESS, userMenuByUerName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USER_MENU_FAIL);
        }
    }
}
